package com.xjrh.traintravel.collector.netty.handler;

import com.xjrh.common.core.utils.DateUtils;
import com.xjrh.traintravel.collector.common.CRC16M;
import com.xjrh.traintravel.collector.common.CodeConvertor;
import com.xjrh.traintravel.collector.controller.param.MvbDataCommandParam;
import com.xjrh.traintravel.collector.netty.coder.command.AgreementCommandEnum;
import com.xjrh.traintravel.collector.netty.coder.command.AgreementConstant;
import com.xjrh.traintravel.collector.netty.coder.command.AgreementStructure;
import com.xjrh.traintravel.collector.netty.command.mvb.MvbData;
import com.xjrh.traintravel.collector.netty.command.mvb.MvbStation;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * MVB-指令处理类
 *
 * @author snps
 * @create 2022-02-07 15:01
 * @describe
 **/
@Service
@Slf4j
public class MvbHandler extends BaseAgreementHandler {

    /**
     * 得到MVB-上报数据
     * @param equipmentId 设备标识
     * @param data 内容
     * @return MvbData
     */
    public MvbData getReportData(String equipmentId, String data) {
        MvbData mvbData = new MvbData();
        mvbData.setEquipmentId(equipmentId);
        mvbData.setCommonNo(data.substring(0, 2));
        mvbData.setCommonData(data.substring(2, 4));
        mvbData.setCommonDataLength(Integer.parseInt(data.substring(4, 6), 16));
        mvbData.setCommonDataContent(data.substring(6, 70));
        mvbData.setSelfData1(data.substring(70, 72));
        mvbData.setSelfData1Length(Integer.parseInt(data.substring(72, 74), 16));
        mvbData.setSelfData1Content(data.substring(74, 138));
        mvbData.setSelfData2(data.substring(138, 140));
        mvbData.setSelfData2Length(Integer.parseInt(data.substring(140, 142), 16));
        mvbData.setSelfData2Content(data.substring(142));
        return mvbData;
    }

    /**
     * 创建命令：回复 上报数据 结果
     * @param agreement 收到的消息
     * @param channel 客户端连接通道
     */
    public void replyGetReportData(AgreementStructure agreement, Channel channel) {
        AgreementStructure replyAgreement = new AgreementStructure();

        // 前导码(4)
        replyAgreement.setPc(AgreementConstant.PC_VALUE);
        // 帧头(1)
        replyAgreement.setSd(AgreementConstant.SD_VALUE);
        // 控制(1)
        replyAgreement.setFc(AgreementCommandEnum.R_88.getValue());
        // MAC地址(6)
        replyAgreement.setAddr(agreement.getAddr());
        // 序号(1)
        // replyAgreement.setSeq(CodeConvertor.intToHexString(1, 1));
        replyAgreement.setSeq(super.getEquipmentSeq(replyAgreement.getAddr()));
        // 长度(2)
        replyAgreement.setLen(CodeConvertor.intToHexString(2, 2));
        // 数据(N)
        replyAgreement.setData("01" + "01");
        // 校验(2)
        String crcValue = CRC16M.calculateCrc(replyAgreement);
        replyAgreement.setFcs(crcValue);
        // 帧尾(1)
        replyAgreement.setFs(AgreementConstant.FS_VALUE);

        // 发送命令
        super.sendCommand(replyAgreement, channel);
    }

    /**
     * 发送：MVB测试数据
     *
     * @param param
     */
    public void sendTestData(MvbDataCommandParam param) {
        AgreementStructure agreement = new AgreementStructure();

        // 前导码(4)
        agreement.setPc(AgreementConstant.PC_VALUE);
        // 帧头(1)
        agreement.setSd(AgreementConstant.SD_VALUE);
        // 控制(1)
        agreement.setFc(AgreementCommandEnum.S_08.getValue());
        // MAC地址(6)
        agreement.setAddr(param.getEquipmentNo());
        // 序号(1)
        // agreement.setSeq(CodeConvertor.intToHexString(1, 1));
        agreement.setSeq(super.getEquipmentSeq(agreement.getAddr()));
        // 数据(N)
        StringBuilder sbuData = new StringBuilder(param.getA0());
        sbuData.append(param.getC0()).append(param.getC1());
        String data = sbuData.toString();
        agreement.setData("05" + "0060" + data);
        // 长度(2)
        agreement.setLen(CodeConvertor.intToHexString(99, 2));
        // 校验(2)
        String crcValue = CRC16M.calculateCrc(agreement);
        agreement.setFcs(crcValue);
        // 帧尾(1)
        agreement.setFs(AgreementConstant.FS_VALUE);

        // 发送命令
        super.sendCommand(agreement);
    }


    /**
     * 得到MVB站点信息
     *
     * @param equipmentId 设备标识
     * @param applyData 响应数据
     * @return MvbStation
     */
    public MvbStation getMvbStation(String equipmentId, String applyData) {
        MvbStation mvbStation = new MvbStation();
        mvbStation.setEquipmentId(equipmentId);
        mvbStation.setCommonNo(applyData.substring(0, 2));
        mvbStation.setTime(applyData.substring(2, 14));
        mvbStation.setRunningDirection(Integer.parseInt(applyData.substring(14, 16), 16) + "");
        mvbStation.setStationCode(Integer.parseInt(applyData.substring(16), 16) + "");

        int time = 0;
        StringBuilder sbuDateTime = new StringBuilder("20");
        time = Integer.parseInt(mvbStation.getTime().substring(0, 2), 16);
        sbuDateTime.append(time>10?time:"0"+time).append("-");
        time = Integer.parseInt(mvbStation.getTime().substring(2, 4), 16);
        sbuDateTime.append(time>10?time:"0"+time).append("-");
        time = Integer.parseInt(mvbStation.getTime().substring(4, 6), 16);
        sbuDateTime.append(time>10?time:"0"+time).append(" ");
        time = Integer.parseInt(mvbStation.getTime().substring(6, 8), 16);
        sbuDateTime.append(time>10?time:"0"+time).append(":");
        time = Integer.parseInt(mvbStation.getTime().substring(8, 10), 16);
        sbuDateTime.append(time>10?time:"0"+time).append(":");
        time = Integer.parseInt(mvbStation.getTime().substring(10, 12), 16);
        sbuDateTime.append(time>10?time:"0"+time);
        mvbStation.setDatetime(sbuDateTime.toString());
        return mvbStation;
    }

}